
var url="/discuss/save";
//选择投票的数量
var vote_num=1;
$(function(){
	//判断是不是发帖人可删除
	var discussUser=$("input[name=discussUser]").val();
	var user=$("input[name=user]").val();
	if (discussUser!=user) {
		$("#delete").hide();
	}
	//讨论区选择排序方式弹出框
	$(".select_sort").click(function(){
		var target=$(this);
		target.next().slideToggle();
	});
	
	//讨论区查看排序规则
	$(".sort-label").click(function(){
		$(".sort-head-right ul").slideToggle();
	});
	
	//评论框出现
	$(".share-bar-answer #comment").click(function(){
		console.log("123");
		var target=$(this);
		target.parent().parent().parent().next().next().toggle();
	});
	//点赞
	$(".share-bar-answer #thumbs").click(function(){
		var target=$(this);
		var col=$(target).attr("data");
		if (col=='yizan') {
			layer.alert("您已操作过！");
			return false;
		}
		var commentId=$(target).attr("data-comment-id");
		$.ajax({
	        type : 'post',
	        url : '/discuss/thumb?commentId=' + commentId,
	        success : function() {
	        	$(target).attr("data","yizan");
	    		target.css("color","#e61818");
	    		var paopao=target.children("#paopao");
	    		paopao.css("display","block");
	    		paopao.animate({
	    			right: -8,top:-80,opacity:'0',
	    		 }, 600);
	    		var thumb_num=$(target).find(".thumb_num").html();
	    		$(target).find(".thumb_num").html(Number(thumb_num)+1);
	        }
	    });
	
	});
	//讨论区发帖选择类型
	$("#discuss_style").change(function(){
		  var content = $("#discuss_style").find("option:selected").text();
		  if(content=="活动"){
			  $("#vote").hide();
			  $(".vote_option").css("display","none");
			  $("#active").show();
			  $("#active1").show();
			  url="/discuss/save/active";
		  }
		  if(content=="投票"){
			  $("#active").hide();
			  $("#active1").hide();
			  $("#vote").show();
			  $(".vote_option").css("display","block");
			  url="/discuss/save/vote";
		  }else if(content=="公告"||content=="讨论"){
			  $("#active").hide();
			  $("#active1").hide();
			  $("#vote").hide();
			  $(".vote_option").css("display","none");
			  url="/discuss/save";
		  }
		  
	});	
	//选择投票
	$(".votes input").change(function(){
		var target=$(this);
		var vote_num=$(".votes input:checked").length;
		var max=$(".votes").attr("name");
		if (vote_num>max) {
			alert("选项不能大于"+max+"次");
			target.prop("checked",false);
			return;
		}
		target.prev().prev().toggle();
		target.prev().toggle();
	});
	
});

/**
	 * 提交帖子表单*/
	function add_discuss(){
	
	$(".add_post").form({
	    url: url,
		onSubmit:function(){
			$("#write_editor").val(CKEDITOR.instances.TextArea1.getData());
			var content = $("#discuss_style").find("option:selected").text();
			 if(content=="活动"){
				 if ($("#post_title .title").val() == "") {
					 document.getElementById("post_title").scrollIntoView();
					 layer.tips('标题不能为空', '#post_title .title');
						return false;
					}
				 if ($(".public input[name=title]").val() == "") {
					 document.getElementById("post_title").scrollIntoView();
					 layer.tips('活动标题不能为空', '.public input[name=title]');
						return false;
					}
				 if ($(".public input[name=address]").val() == "") {
					 document.getElementById("active_address").scrollIntoView();
					 layer.tips('活动地点不能为空', '.public input[name=address]');
						return false;
					}
				 if ($(".public input[name=startTime]").val() == "") {
					 document.getElementById("active_start_time").scrollIntoView();
					 layer.tips('活动开始时间不能为空', '.public input[name=startTime]');
						return false;
					}
				 if ($(".public input[name=endTime]").val() == "") {
					 document.getElementById("active_end_time").scrollIntoView();
					 layer.tips('活动结束时间不能为空', '.public input[name=endTime]');
						return false;
					}
				  if ($("#write_editor").val() == "") {
						 layer.tips('内容不能为空', '.write_editor');
						return false;
					}
			  }
			  if(content=="投票"){
				  if ($("#post_title .title").val() == "") {
						 document.getElementById("post_title").scrollIntoView();
						 layer.tips('标题不能为空', '#post_title .title');
							return false;
						}
				  if ($(".public input[name=title]").val() == "") {
					  	document.getElementById("post_title").scrollIntoView();
						 layer.tips('投票标题不能为空', '.public input[name=title]');
						return false;
					}
				  if ($(".public input[name=vendTime]").val() == "") {
					  document.getElementById("vote_time").scrollIntoView();
						 layer.tips('投票截止时间不能为空', '.public input[name=vendTime]');
						return false;
					}
				  var num=0;
				  $(".options input").each(function(){
					  var vl=$(this).val();
					  if(vl!="")
					  {
						 num++; 
					  }
					  });
				  if(num<2){
					  layer.msg('选项内容不能少于两个！');
					  return false;
				  }
				  if ($("#write_editor").val() == "") {
					  layer.tips('内容不能为空', '.write_editor');
						return false;
					}
			  }
			  if(content=="公告"||content=="讨论"){
				  if ($("#post_title .title").val() == "") {
					 document.getElementById("post_title").scrollIntoView();
					 layer.tips('标题不能为空', '#post_title .title');
						return false;
					}
				  if ($("#write_editor").val() == "") {
					  layer.tips('内容不能为空', '.write_editor');
						return false;
					}
			  }
			  
		},
		success:function(data){
			layer.msg('发布成功',{shift: -1,time:1000}, function(){
				refreshPanel(getUrlPrefix("/discuss?comment=0"),"论坛");
			});
	    }
	});
}

//投票选项增加
function add_option(){
	var index=$(".options").children().length;
	if (index>10) {
		alert("最多添加10个选项!");
		return false;
	}
	$(".options .add_option").before("<div class='option' date='"+index+"'><input type='text' name='voteContents["+(index-1)+"].vcontent' placeholder='选项"+index+"'><span onclick='delete_option(this)'><i class='iconfont icon-jian'></i></span></div>");
}
//删除投票选项
function delete_option(e){
	$(e).parent().nextAll(".option").each(function(){
		var sort=$(this).attr("date");
		$(this).attr("date",""+(sort-1)+"");
		$(this).children().attr("name","voteContents["+(sort-1)+"].vcontent");
		$(this).children().attr("placeholder","选项"+(sort-1)+"");
		console.log($(this).children().attr("name"));
	});
	$(e).parent().remove();
}
//回复内容填写
function write_comment_content(e){
	var myDate = new Date();
	//获取当前年
	var year=myDate.getFullYear();
	//获取当前月
	var month=myDate.getMonth()+1;
	//获取当前日
	var date=myDate.getDate(); 
	var comment=$(e).prev().val();
	if (comment=="") {
		layer.tips('回复内容不能为空',  $(e).prev(), {
			  tips: [1, 'black'], //还可配置颜色
			});
		return false;
	}
	var commentId=$(e).parents(".comments").attr("data");
	var contentReply=$(e).prev().val();
	var myphoto=$("input[name=myphoto]").val();
	$.ajax({
        type : 'post',
        url : '/discuss/reply?commentId=' + commentId + '&contentReply=' + contentReply,
        success : function() {
            	layer.msg('回复成功',{shift: -1,time:1000}, function(){
            		var comment_num=$(e).parent().prev().prev().find(".comment_num").html();
                	$(e).parent().prev().prev().find(".comment_num").html(Number(comment_num)+1);
                	$(e).parent().append("<dl class='user_comments'><dt><img src='/file/image/"+myphoto+"' style='width: 30px;height: 30px;border-radius: 50%;'></dt><dd><span>"+$(e).parents(".comments").attr("data-name")+"</span>回复："+comment+"</dd><dd class='comments_time'>"+year+"-"+month+"-"+date+"</dd></dl>");
                	$(e).prev().val("");
                	
    			});
        }
    });
	
}

//确定投票

$("#commitid").form({
    url: "/discuss/add/vote",
	onSubmit:function(){
		var chk_value =[]; 
		$('.votes input:checked').each(function(){ 
		chk_value.push($(this).val()); 
		}); 
		if (chk_value.length==0) {
			layer.tips('请选择选项', '.votes', {
				  tips: [1, 'black'] //还可配置颜色
				});
			return false;
		}
	},
	success:function(data){
			layer.msg('投票成功',{shift: -1,time:1000}, function(){
				var url=$(".votes").attr("data-url");
				refreshPanel(getUrlPrefix(url),"查看帖子");
			});
	    }
	});

var discussId="";
var commentBackUrl="";
function comment_url(){
	var type=$(".link_comment").attr("data");
	discussId=$(".link_comment").attr("discussId");
	if (type==0||type==1) {
		commentBackUrl="/discuss/view?"+"id="+discussId+"";
	}else if(type==2){
		commentBackUrl="/discuss/view/vote?"+"id="+discussId+"";
	}else if(type==3){
		commentBackUrl="/discuss/view/active?"+"id="+discussId+"";
	}
}

//评价
	$("#mycomment").form({
	    url: "/discuss/comment",
		onSubmit:function(){
			if ($("#comment_content").val() == "") {
				  layer.tips('内容不能为空', '#comment_content');
					return false;
				}
		},
		success:function(data){
				layer.msg('评论成功',{shift: -1,time:1000}, function(){
					refreshPanel(getUrlPrefix(commentBackUrl),"查看帖子");
				});
		    }
		});

//查找关键字
	var key=null;
	$("#discussSearch").form({
		url:"/discuss/search",
		onSubmit:function(){
			key=$("input[name=key]").val();
		},
		success:function(data){
			refreshPanel(getUrlPrefix("/discuss/search?key="+key),"查看帖子");
		}
	});

//删除帖子
	function delete_discuss(e){
		var discussUser=$("input[name=discussUser]").val();
		var user=$("input[name=user]").val();
		/*if (discussUser!=user) {
			//layer.alert("您没有此权限！");
			$(e).hide();
			return false;
		}*/
		layer.confirm('删除后不可恢复，确认删除？',{closeBtn: 0},function(index){
			var type=$(".link_comment").attr("data");
			discussId=$(".link_comment").attr("discussId");
			$.ajax({
		        type : 'get',
		        url : '/discuss/delete?discussId=' + discussId,
		        success : function() {
		        	 layer.close(index);
		        	refreshPanel(getUrlPrefix("/discuss?comment="+type),"讨论区");
		        }
		    });
		});
	}















